//
//  FirstMainTableViewCell.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/11/23.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FirstMainTableViewCell.h"
#import "FirstModel.h"
#import "UIImageView+WebCache.h"

@interface FirstMainTableViewCell ()

@property (nonatomic, retain) UIImageView *image;//大图
@property (nonatomic, retain) UILabel *title;//标题
@property (nonatomic, retain) UILabel *subhead;//副标题
@property (nonatomic, retain) UILabel *nickname;//昵称
@property (nonatomic, retain) UIImageView *headicon;//头像
@property (nonatomic, retain) UIView *layerView;
@property (nonatomic, retain) CAGradientLayer *gradientLayer;

@end

@implementation FirstMainTableViewCell

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.image = [[UIImageView alloc] init];
        [self.contentView addSubview:self.image];
        self.image.userInteractionEnabled = YES;
        
        //初始化渐变
        self.layerView = [[UIView alloc] init];
        self.gradientLayer = [CAGradientLayer layer];
        self.gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColor grayColor].CGColor,(id)[UIColor clearColor].CGColor, nil];
        self.gradientLayer.startPoint = CGPointMake(0.01, 0.01);
        self.gradientLayer.endPoint = CGPointMake(0.03, 0.3);
        [self.layerView.layer insertSublayer:self.gradientLayer atIndex:0];
        [self.contentView addSubview:self.layerView];
        
        self.subhead = [[UILabel alloc] init];
        [self.contentView addSubview:self.subhead];
        
        self.nickname = [[UILabel alloc] init];
        [self.contentView addSubview:self.nickname];
        
        self.headicon = [[UIImageView alloc] init];
        [self.contentView addSubview:self.headicon];
        
        
        self.title = [[UILabel alloc] init];
        [self.contentView addSubview:self.title];
        
    }
    return self;
}
#pragma mark - 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.image.frame = CGRectMake(5, 5, self.contentView.frame.size.width - 10, 190);
//    self.image.layer.cornerRadius = 5;
    self.image.clipsToBounds = YES;
    
    self.title.textColor = [UIColor whiteColor];
    self.title.font = [UIFont systemFontOfSize:20];
    self.title.shadowColor = [UIColor blackColor];
    self.title.shadowOffset = CGSizeMake(1, 1);
    self.title.frame = CGRectMake(15, 15, self.contentView.frame.size.width - 30, 20);
    
    self.subhead.frame = CGRectMake(15, self.title.frame.origin.y + self.title.frame.size.height + 10, 200, 15);
    self.subhead.font = [UIFont systemFontOfSize:13];
    self.subhead.shadowColor = [UIColor blackColor];
    self.subhead.textColor = [UIColor whiteColor];
    self.subhead.shadowOffset = CGSizeMake(1, 1);
    
    self.headicon.frame = CGRectMake(15, 200 - 50, 30, 30);
    
    
    self.nickname.frame = CGRectMake(self.headicon.frame.origin.x + self.headicon.frame.size.width + 5, 200 - 40, 200, 15);
    self.nickname.font = [UIFont systemFontOfSize:12];
    self.nickname.textColor = [UIColor whiteColor];
    self.nickname.shadowColor = [UIColor grayColor];
    
    //设置frame
    self.layerView.frame = self.image.frame;
    self.gradientLayer.bounds = self.layerView.bounds;
    self.gradientLayer.frame = self.layerView.bounds;
    
}
#pragma mark - 赋值
- (void)setFirstModel:(FirstModel *)firstModel
{
    if (_firstModel != firstModel) {
        _firstModel = firstModel;
    }
    
//    if (!firstModel.title.length) {
//        [self.image sd_setImageWithURL:[NSURL URLWithString:firstModel.image] placeholderImage:[UIImage imageNamed:@"BG"]];
//        self.title.text = @"";
//        self.subhead.text = @"";
//        self.nickname.text = @"";
//        [self.headicon sd_setImageWithURL:[NSURL URLWithString:@""]];
//        self.headicon.layer.borderWidth = 0;
//    } else {
        //标题
        self.title.text = firstModel.title;
        //副标题
        self.subhead.text = firstModel.subhead;
        //昵称
        self.nickname.text = [NSString stringWithFormat:@"by %@", firstModel.nickname];
        //头像
        [self.headicon sd_setImageWithURL:[NSURL URLWithString:firstModel.headicon] placeholderImage:[UIImage imageNamed:@"touxiang"]];
//        self.headicon.layer.cornerRadius = 5;
        self.headicon.clipsToBounds = YES;
        self.headicon.layer.borderWidth = 1;
        self.headicon.layer.borderColor = [UIColor whiteColor].CGColor;
        //大图
        [self.image sd_setImageWithURL:[NSURL URLWithString:firstModel.image] placeholderImage:[UIImage imageNamed:@"BG"]];
//    }
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
